package getBatterySample;

import com.opencsv.CSVReader;
import com.opencsv.exceptions.CsvException;

import java.io.*;
import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.List;

/**
 * @projectName: code-snippet
 * @className: BatteryMeasure
 * @description: 读取自动化测试日志，抓取battery_measure_sample数值，转换为高低负载电压值，并输出至文档
 * @author: JLONG
 * @date: 2024/11/29 13:47
 * @version: 1.0
 */
public class BatteryMeasure {
    public static void main(String[] args) {
        BatteryMeasure.grabMeasureSample("/Users/jlong/IdeaProjects/code-snippet/FT_fg22+2340_3.2.0_3.9.0_chan_9_others_.csv", "BatteryMeasureSample.csv");
    }

    public static void grabMeasureSample(String inputFilePath, String outputFilePath) {
        grabMeasureSample(new File(inputFilePath), new File(outputFilePath));
    }

    public static void grabMeasureSample(File inputFile, File outputFile) {
        System.out.println("程序启动:");
        List<SampleBeen> list = new ArrayList<>();
        try {
            CSVReader reader = new CSVReader(new FileReader(inputFile));
            List<String[]> records = reader.readAll();
            for (String[] recordArray : records) {
                for (String str : recordArray) {
                    if (str.equals("BATTERY_MEASUREMENT_SAMPLE")) {
                        String labelID = recordArray[6];
                        String readableSensorValue = recordArray[19].substring(3);
                        list.add(new SampleBeen(labelID, readableSensorValue));
                    }
                }
            }
            reader.close();
            writeMeasureSample(list, outputFile);
            System.out.println("程序结束，结果请查看文件：" + outputFile.getAbsolutePath());
        } catch (CsvException | IOException e) {
            throw new RuntimeException(e);
        }
    }

    private static void writeMeasureSample(List<SampleBeen> list, File outputFile) {
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter(outputFile));
            System.out.println("Label ID,SampleValue,LowLoadVoltage,HighLoadVoltage,Result");
            bw.write("Label ID,SampleValue,LowLoadVoltage,HighLoadVoltage,Result" + "\n");
            for (SampleBeen sampleBeen : list) {
                String labelID = sampleBeen.getLabelID();
                String sampleValue = sampleBeen.getSampleValue();
                double lowLoadVoltage = sampleBeen.getLowLoadVoltage();
                double highLoadVoltage = sampleBeen.getHighLoadVoltage();
                String resultValue = sampleBeen.getResult();
                String printInfo = labelID + "," + sampleValue + "," + lowLoadVoltage + "," + highLoadVoltage + "," + resultValue;
                System.out.println(printInfo);
                bw.write(printInfo + "\n");
            }
            bw.flush();
            bw.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
